package com.hw.blog.common;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//import com.aaplus4pc.common.BaseAction;
//import com.aaplus4pc.common.CommonException;
//import com.opensymphony.xwork2.ActionContext;

/**
 * String 유틸리티 클래스
 * @version 1.0
 * @since   2012.03.27
 * @author  eyoh
 */
public class WebUtil {
	
	 
	static Log logger = LogFactory.getLog(WebUtil.class);
	
	
	public static String str2alert(String s) {
		if (s == null) {
			return null;
		}
		StringBuffer buf = new StringBuffer();
		char[] c = s.toCharArray();
		int len = c.length;
		for (int i = 0; i < len; i++) {
			if (c[i] == '\n') {
				buf.append("\\n");
			} else if (c[i] == '\t') {
				buf.append("\\t");
			} else if (c[i] == '"') {
				buf.append(" ");
			} else if (c[i] == '\'') {
				buf.append("\\'");
			} else if (c[i] == '\r') {
				buf.append("\\r");
			} else {
				buf.append(c[i]);
			}
		}
		return buf.toString();
	}
	
	public static String encode( String str ) {
		return encode( str ,"EUC-KR" );
	}

	public static String encode( String str ,String charset ){
		String returnStr = null;
		if( str != null ) {
			try {
				returnStr = URLEncoder.encode( str ,charset );
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	
		return returnStr;
	}
	/**
	 * 나누기 
	 * @param 분자 val, 분모divVal
	 * @return
	 */
	public static String divAmount(Object val, BigDecimal divVal){ 
//		String result = "-";
		String result = "0";
		String strVal = StringUtil.nullCheck(val);
		if(!"0".equals(strVal)){
			
			BigDecimal chkVal = new BigDecimal(strVal).divide(divVal,3, BigDecimal.ROUND_HALF_UP);
			chkVal= chkVal.multiply(new BigDecimal(100));
			chkVal = chkVal.setScale(1, BigDecimal.ROUND_HALF_UP);
		    result = StringUtil.nullCheck(chkVal); 
		}
		return result;
	}

	public static String decode( String str ) {
		return decode( str ,"EUC-KR" );
	}
	
	public static String decode_utf_8( String str ) {
		return decode( str ,"UTF-8" );
	}

	public static String decode( String str ,String charset ){
		String returnStr = null;
		if( str != null ) {
			try {
				returnStr = URLDecoder.decode( str ,charset );
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	
		return returnStr;
	}

	public static String baseEnCoder(String str){
		String returnStr = "";
		try {
			returnStr = new sun.misc.BASE64Encoder().encode(str.getBytes());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return returnStr;
	}
}
